﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class uc_NgonNgu : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_NgonNgu()
        {
            InitializeComponent();
        }
        NgonNgu_BLL ngonNgu_bll = new NgonNgu_BLL();
        private void uc_NgonNgu_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = ngonNgu_bll.LayDuLieu();
            splitContainerControl1.Hide();
            bbi_Luu.Enabled = false;

        }
        int flag;
        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 0;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Xoa.Enabled = false;
            bbi_Luu.Enabled = true;
            splitContainerControl1.Show();
        }


        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Xoa.Enabled = false;
            bbi_Luu.Enabled = true;
            txt_MaNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Ngôn Ngữ").ToString();
            tg.XXX=txt_tenNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Ngôn Ngữ").ToString();
            splitContainerControl1.Show();
        }
        private void bbi_Xem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 2;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = false;
            bbi_Xoa.Enabled = true;
            txt_MaNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Ngôn Ngữ").ToString();
            tg.XXX= txt_tenNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Ngôn Ngữ").ToString();
            splitContainerControl1.Show();
        }
        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            flag = 2;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = false;
            bbi_Xoa.Enabled = true;
            bbi_Luu.Enabled = false;
            txt_MaNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Ngôn Ngữ").ToString();
            tg.XXX=txt_tenNN.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Ngôn Ngữ").ToString();
            splitContainerControl1.Show();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NgonNgu_Public nn = new NgonNgu_Public();

            if (flag == 0)
            {
             //them
                nn.TenNgonNgu = txt_tenNN.Text;
                ngonNgu_bll.insert_NgonNgu(nn);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Ngôn ngữ: '" + txt_tenNN.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                //sua
                nn.MaNgonNgu = int.Parse(txt_MaNN.Text);
                nn.TenNgonNgu = txt_tenNN.Text;
                ngonNgu_bll.Update_NgonNgu(nn,nn);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Ngôn ngữ: '" +tg.XXX + "' thành: '" + txt_tenNN.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (flag == 2)
            {
            //xem
                nn.MaNgonNgu = int.Parse(txt_MaNN.Text);
                nn.TenNgonNgu = txt_tenNN.Text;
                ngonNgu_bll.Update_NgonNgu(nn, nn);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Ngôn ngữ: '" + tg.XXX + "' thành: '" + txt_tenNN.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_NgonNgu_Load(sender,e);
            splitContainerControl1.Show();
            txt_MaNN.ResetText();
            txt_tenNN.ResetText();
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Xoa.Enabled = true;
            bbi_Luu.Enabled = false;
        }


        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            //string chuoi=comboBox1.Text,a,b;
            //string[] tachChuoi =chuoi.Split('-');
            //a=tachChuoi[0];
            //b=tachChuoi[1];
            //XtraMessageBox.Show("số 1 '"+a+"' va so 2: '"+b+"'");

            splitContainerControl1.Hide();
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Xoa.Enabled = true;
            bbi_Luu.Enabled = false;
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            txt_MaNN.ResetText();
            txt_tenNN.ResetText();
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Xem.Enabled = true;
            bbi_Xoa.Enabled = true;
            bbi_Luu.Enabled = false;
        }

        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NgonNgu_Public nn = new NgonNgu_Public();
            nn.MaNgonNgu =Convert.ToInt32( gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Ngôn Ngữ").ToString());
            nn.TenNgonNgu = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Ngôn Ngữ").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Ngôn ngữ: '" + nn.TenNgonNgu + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                ngonNgu_bll.Delete_NgonNgu(nn);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Ngôn ngữ: '" + nn.TenNgonNgu + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                uc_NgonNgu_Load(sender, e);
            }
        }

        private void txt_MaNN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
    }
}
